/*
 ============================================================================
 文件名 	: EasyBounceGameView.cpp
 描述		: EasyBounceGameView类的实现.
 ============================================================================
 */

//引用
#include <coemain.h>
#include <viewcli.h>
#include <aknviewappui.h>
#include <aknmessagequerydialog.h>
#include <aknnotewrappers.h>
#include <stringloader.h>
#include <EasyBounce.rsg>

#include "EasyBounceAppUi.h"
#include "EasyBounceGameView.h"
#include "EasyBounceGameContainer.h"
#include "EasyBounce.hrh"

// ============================ 成员函数 ===============================

// ---------------------------------------------------------
// CFileFavorGameView::ConstructL()
// 两阶段构造函数.
// ---------------------------------------------------------
//
void CEasyBounceGameView::ConstructL()
    {
    BaseConstructL( R_EB_GAMEVIEW );
    
    iScoreCounter = CScoreCounter::NewL();
    iAudioPlayer = CGameAudioPlayer::NewL();
    }

// ---------------------------------------------------------
// CEasyBounceGameView::CEasyBounceGameView()
// C++默认构造函数.
// ---------------------------------------------------------
//
CEasyBounceGameView::CEasyBounceGameView():iEngine( NULL ),
                                    iScoreCounter( NULL ),
                                    iAudioPlayer( NULL )
	{
	
	}

// ---------------------------------------------------------
// CEasyBounceGameView::~CEasyBounceGameView()
// 析构函数.
// ---------------------------------------------------------
//
CEasyBounceGameView::~CEasyBounceGameView()
    {
    if ( iContainer )
        {
        AppUi()->RemoveFromViewStack( *this, iContainer );
        }
    if( iEngine )
    	{
    	iEngine->Cancel();
    	iEngine->Destroy();
    	iEngine = NULL;    
    	}
    delete iAudioPlayer;
    iAudioPlayer = NULL;
    
    delete iScoreCounter;
    iScoreCounter = NULL;
    
	delete iContainer;
    iContainer=NULL;
    }

// ---------------------------------------------------------
// CEasyBounceGameView::Id()
// 返回视图ID
// ---------------------------------------------------------
//
TUid CEasyBounceGameView::Id() const
    {
    return KGameViewId;
    }

// ---------------------------------------------------------
// CEasyBounceGameView::HandleCommandL()
// 处理GameView对应的菜单命令 
// ---------------------------------------------------------
//
void CEasyBounceGameView::HandleCommandL(TInt aCommand)
    {   
    switch ( aCommand )
        {
        //退出程序
        case EAknSoftkeyExit:
        	{
        	if( iEngine )
        		{
        		iEngine->Cancel();
        		iEngine->Destroy();
        		iEngine = NULL;
        		}
        	AppUi()->ActivateLocalViewL( TUid::Uid( EMainViewId ) );
        	break;
        	}
        //暂停游戏
        case ECmdPause:
        	{
        	if( iEngine )
        		{
        		iEngine->PauseOrResume();
        		}
        	break;
        	}
        //开始新游戏
        case EAknSoftkeyStart:
        	{
			iEngine->Destroy();
			iEngine = NULL;
			iEngine = CEngine::Instance();
			iEngine->AttachObserver( iContainer );
			iScoreCounter->SetScore( 0 );
			iEngine->AttachObserver( iScoreCounter );
		    iEngine->AttachObserver( iAudioPlayer );
			iEngine->Initialize();
			iEngine->Next();
			break;
        	}
        //游戏结束
        case ECmdOver:
            {
            TInt score = iScoreCounter->GetScore();
            TBuf<50> buf;
            buf.Format( _L("Game Over! You Scored %d."), score );
            CAknInformationNote* note = new (ELeave)CAknInformationNote( EFalse );
            note->ExecuteLD( buf );

            // TODO switch to rank view
            //AppUi()->ActivateLocalViewL( TUid::Uid( ERankViewId ) );
            }
            break;
        //其他命令交给AppUi处理
        default:
            {
            AppUi()->HandleCommandL( aCommand );
            break;
            }
        }
    }

// ---------------------------------------------------------
// CEasyBounceGameView::HandleViewRectChange()
// ---------------------------------------------------------
//
void CEasyBounceGameView::HandleViewRectChange()
    {
    if ( iContainer )
        {
        iContainer->SetRect( ClientRect() );
        }
    }

// ---------------------------------------------------------
// CEasyBounceGameView::DoActivateL()
// 此函数在视图被激活时调用
// 视图所包含的container在此创建
// 视图被激活时可以接收消息，通过后两个参数来传递
// 参数aPrevViewId，上一个视图的ID
// 参数aCustomMessageId，消息ID
// 参数aCustomMessage，消息内容
// ---------------------------------------------------------
//
void CEasyBounceGameView::DoActivateL(
   const TVwsViewId& /*aPrevViewId*/, TUid /*aCustomMessageId*/,
   const TDesC8& aCustomMessage)
    {
    if ( !iEngine )
    	{
    	iEngine = CEngine::Instance();
    	}
    if (!iContainer)
        {
        iContainer = CEasyBounceGameContainer::NewL();
        iContainer->SetRect( ClientRect() );
        iContainer->SetMopParent(this);
        AppUi()->AddToStackL( *this, iContainer );
        iEngine->AttachObserver( iContainer );
        }
    
    //测试代码
    CEasyBounceAppUi* appUi = static_cast<CEasyBounceAppUi*>( AppUi() );
    TBuf<10> level;
    level.Copy( aCustomMessage );
    appUi->ShowMessage( level, EInformation );
   }

// ---------------------------------------------------------
// CEasyBounceGameView::DoDeactivate()
// 视图被反激活时调用此函数
// 此函数里进行对该视图拥有的container进行销毁
// ---------------------------------------------------------
//
void CEasyBounceGameView::DoDeactivate()
    {
    if ( iContainer )
    	{
        AppUi()->RemoveFromViewStack( *this, iContainer );
        }
    delete iContainer;
    iContainer = NULL;
    }

// ---------------------------------------------------------
// CEasyBounceGameView::Engine()
// 返回游戏引擎指针
// ---------------------------------------------------------
//
CEngine* CEasyBounceGameView::Engine()
	{
	return iEngine;
	}
//文件结束
